package com.digital.cnzz.zkp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.cnzz.zkp.service.ICalculationExpressConfigService;
import com.digital.cnzz.zkp.ao.*;
import com.digital.cnzz.zkp.common.constants.ResponseEnum;
import com.digital.cnzz.zkp.common.vo.VO;
import com.digital.cnzz.zkp.constant.DeletedStatusEnum;
import com.digital.cnzz.zkp.constant.IsActiveEnum;
import com.digital.cnzz.zkp.entity.CalculationExpressConfig;
import com.digital.cnzz.zkp.mapper.CalculationExpressConfigMapper;
import com.digital.cnzz.zkp.page.CommonPage;
import com.digital.cnzz.zkp.vo.CalculationExpressConfigVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author liujiang
 * @date 2021/3/30 17:28
 */
@Service
public class CalculationExpressConfigServiceImpl extends ServiceImpl<CalculationExpressConfigMapper, CalculationExpressConfig> implements ICalculationExpressConfigService {
    @Autowired
    CalculationExpressConfigMapper calculationExpressConfigMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public VO saveCalculationExpressConfig(CalculationExpressConfigSaveAO ao) {

        CalculationExpressConfig calculationExpressConfig = new CalculationExpressConfig();
        if (null == ao.getId()) {
            //新增
            BeanUtil.copyProperties(ao, calculationExpressConfig);
            calculationExpressConfig.setIsDelete(DeletedStatusEnum.NOTDELETED.getCode());
            calculationExpressConfig.setIsActive(IsActiveEnum.NO_ACTIVE.getCode());
        } else {
            //更新
            BeanUtil.copyProperties(ao, calculationExpressConfig);
        }
        boolean b = saveOrUpdate(calculationExpressConfig);
        if (!b) {
            return VO.response(ResponseEnum.FAIL);
        }
        return VO.success();
    }

    /**
     * 删除计算规则
     *
     * @param ao
     */
    @Override
    public void deleteById(CalculationExpressDelAO ao) {
        CalculationExpressConfig calculationExpressConfig = new CalculationExpressConfig();
        calculationExpressConfig.setId(ao.getId());
        calculationExpressConfig.setIsDelete(DeletedStatusEnum.DELETED.getCode());
        calculationExpressConfigMapper.updateById(calculationExpressConfig);
    }

    /**
     * 获取规则列表
     *
     * @param params 查询参数
     * @return
     */
    @Override
    public CommonPage<CalculationExpressConfigVO> getTaskPageList(CalculationExpressConfigPageListAO params) {
        CommonPage<CalculationExpressConfigVO> commonPage = new CommonPage<>();
        Integer pageNum = params.getPageNum();
        Integer size = params.getPageSize();
        IPage<CalculationExpressConfigVO> page = new Page<>(pageNum, size);
        page = calculationExpressConfigMapper.getCalculationExpressPageList(page, params);
        if (null != page) {
            List<CalculationExpressConfigVO> list = page.getRecords();
            commonPage.setTotal(page.getTotal());
            commonPage.setTotalPage(page.getPages());
            commonPage.setPageSize(page.getSize());
            commonPage.setPageNum(page.getCurrent());
            commonPage.setList(list);
        }
        return commonPage;
    }

    @Override
    public CalculationExpressConfig getExpressConfigById(Long id) {
        QueryWrapper<CalculationExpressConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CalculationExpressConfig::getId, id);
        CalculationExpressConfig one = getOne(queryWrapper);
        return one;
    }

    @Override
    public boolean updateIsActive(Long id) {
        CalculationExpressConfig expressConfig = getExpressConfigById(id);
        Integer isActive = expressConfig.getIsActive();
        if (IsActiveEnum.ACTIVE.getCode().equals(isActive)) {
            expressConfig.setIsActive(IsActiveEnum.NO_ACTIVE.getCode());
        } else {
            expressConfig.setIsActive(IsActiveEnum.ACTIVE.getCode());
        }
        int i = calculationExpressConfigMapper.updateById(expressConfig);
        return i > 0;
    }
}
